package module::1_gucci_fr::parser;
use Encode;
use Encode::JP::H2Z;
use strict;
use Time::localtime;
use 5.010001;

# ---------------------------------------------
# constructor
# 11/23/2012 Louise create
# ---------------------------------------------
sub new {
	my $class = shift;
	my $self  = {};
	bless $self, $class;
	return $self;
}

# ---------------------------------------------
# parse one product detail page
# 11/23/2012 Louise create
# ---------------------------------------------
sub parseAndgetInfo {
	my $this = shift;
	my $url  = shift;
	my $name = shift;
	my $charset = 'utf-8';
	my $content = $main::filedata->getPage($url, $charset);
	my $money = decode ('utf8', '€');
	my $splitString = '<Split>';
	
	if ( !$content ) {
		# E300001 : Failed to get detail information of XXX.
		Log::Writer::setLog( 'E300001', { 'URL' => $url } );
		return;
	}else{
		$content =~ s/<!--.*?-->//isg;
	}

	# defind properties about product
	my %product;

	my $productName;
	my $price;
	my $salePrice;
	my $description;
	my $imageURL;
	my @sizes;
	my @colors;
	my $size;
	my $instock;
	my %colorNames;
	my @productIds;
	my $productId;
	my @prices;
	
	# get (ProductID) and color info
	if ($content =~ /div[^>]*id="variations[^"]*"[^>]*>(.*?)<\/section>/is){
		my $besoin = $1;
		$besoin =~ s/<!--.*?-->//isg;
		
		while($besoin =~ /<li[^>]*class="[^"]*"[^>]*title="([^"]*)"[^>]*>.*?<a[^>]*href="\/fr\/styles\/([^"]*)".*?src="([^"]*)"/isg){
			my %color;
			my $colorName = $1;
			my $colorNameCount = $1;
			$productId = $2;
			my $swatchUrl = $3;
			$colorName =~ s/^\s*|\s*$//isg;
			$imageURL = $swatchUrl;
			$imageURL =~ s/variation/full/isg;
			
			if(!$imageURL){
				Log::Write::setLog( 'E300002', { 'URL' => $url, 'POINT' => 'ImageURL' } );
				return;	
			}
			#delete no name product color
			if (!$colorName){
				next;
			}
			
			#array of the productId
			if (@productIds ~~ /$productId/) {
				next;
			}
			push (@productIds, $productId);
			
			$colorNames{$colorNameCount} = $colorNames{$colorNameCount} + 1;
			
			#if exist the duplicated color name
			if ($colorNames{$colorNameCount} > 1){
				$colorName = $colorName.(--$colorNames{$colorNameCount});
				$colorNames{$colorNameCount}++;
		    }

		    #if color name is too long, need only 40 chars
		    if (length($colorName) > 40){
		    	my $long = 0;
		    	my $currentNum = $colorNames{$colorNameCount} - 1;

		    	if (!$currentNum){
		    		$colorName = substr ($colorName, 0, 37);
		    		$colorName = $colorName.'...';
		    	} else {
		    		$long = length($currentNum);
		    		$colorName = substr ($colorName, 0, (40 - ($long +3)));
		    		$colorName = $colorName.$currentNum.'...';
		    	}
		    }

			#push color node info to the color array
			$color{'Name'} = $colorName;
			$color{'ImageURL'} = $imageURL;
			$color{'SwatchURL'} = $swatchUrl;
			push (@colors, \%color);
		}
		
		#compare product Id
		$productId = &minProductId(@productIds);
	}
		
	# get price, size and description
	foreach my $tempId(@productIds) {
		my $pricesizeUrl = "http://www.gucci.com/fr/styles/$tempId/2/populate_dynamic_content";
		
		my $pricesizecontent = $main::filedata->getPage($pricesizeUrl, $charset);
		if($pricesizecontent =~ /"price":"(\d+\\u20ac)",/is){
			$price = $1;
			$price =~ s/\\u20ac//isg;#€
		}
		if (!(@prices ~~ $price)){
			push @prices, $price;
		}
				
		if(!$price){
			Log::Write::setLog( 'E300002', { 'URL' => $url, 'POINT' => 'Price' } );
			return;		
		}
	
		# get size
		while($pricesizecontent =~ /"buyable_status":(.*?),"status_message":"[^"]*","size":"([^"]*)",/isg){
			$instock = $1;
			$size = $2;

			if($instock =~ /true/is && !(@sizes ~~ /$size/)){
				push @sizes, $size;
			} else{
				Log::Write::setLog('E300002', {'URL' => $url,'POINT'=>'Size'});
			}
		}
	}
	#compare product price
	my ($pricelow, $pricehigh) = &twoPrice(@prices);
	if ($pricelow == $pricehigh||!$pricehigh){
		$price = $pricelow.$money;
	}
	elsif($pricehigh){
		$price = $pricelow.$money.'-'.$pricehigh.$money;
	}
	
    # get Description
    if ($content =~ /<div[^>]*id="description" >\s*<div>(?:<div class='no_bullet_point'>(.*?)<\/div>)?<ul>(.*?)<\/ul><\/div>/is){
    	
    	if ($1 eq "") {
    		$description = $2;
    	} else {
    		$description = $1.$2;
    	}
    	
    	$description =~ s/<li><\/li>//isg;
    	$description =~ s/^\s*|\s*$//isg;
    }
#	my $des_url = "http://www.gucci.com/fr/styles/$productId/load_style.js";
#	my $des_conte = $main::filedata->getPage($des_url, $charset);
#
#	if ($des_conte =~ /"bullets":"(.*?)",/is){
#		$description = $1;
#		$description =~ s/<li><\/li>//isg;
#	}
	
	if( !$description ) {
		$description = $name;
		Log::Write::setLog( "R300002", { 'URL' => $url } );
	}
	
	$productName = $name;
	$productName =~ s/&quot;/'/isg;
	if(!$productName){
		Log::Write::setLog( "E300002", { 'URL' => $url, 'POINT' => 'ProductName' } );
		return;	
	}
		
	%product = (
		'ProductId'   => $productId,
		'Name'        => $productName,
		'Price'       => $price,
#		'SalePrice'   => $salePrice,
		'Description' => $description,
		'ImageURL'    => $imageURL,
		'Size'        => \@sizes,
		'Color'       => \@colors,
		'Time'        => Date::Time::getGMT()
	);
	return %product;
}

# get description sub
#sub getDescription {
#	my $isMatch = shift;
#	my $desc = '';
#	if ($isMatch) {
#		$desc = $1;
#		$desc =~ s/^\s+|\s+$//isg;
#		$desc =~ s/<li><\/li>//isg;
#	}
#	return $desc;
#}

#compare the productId
sub minProductId {
	my $min = shift @_;
	foreach (@_) {
		if ($_ le $min) {
			$min = $_;
		}
	}
	$min;
}

sub twoPrice {
	my $minprice = shift @_;
	my $maxprice;
	foreach (@_){
		if ($_ < $minprice){
			$minprice = $_;
			$maxprice = $minprice;
		}
		elsif($_ >= $minprice){
			$maxprice = $_;
		}
	}
	return ($minprice, $maxprice);
}

1;